//
//  YCCommitInterviewViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/5.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  提交预约考试

#import "YCCommitInterviewViewController.h"
#import "YCExamTimeField.h"
#import "YCBookTimeModel.h"
#import "YCBookCourseModel.h"
#import "YCInterviewCommitSucceedView.h"

@interface YCCommitInterviewViewController ()<UITextFieldDelegate>
/** scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
/** containerView */
@property (nonatomic, strong) UIView *containerView;
/** 考试科目输入  */
@property (nonatomic, weak) UITextField *examCourseField;
/** 考试时间输入  */
@property (nonatomic, weak) UITextField *examTimeField;
/** 确认提交按钮  */
@property (nonatomic, strong) UIButton *confirmCommitButton;

/** 提交成功view  */
@property (nonatomic, strong) YCInterviewCommitSucceedView *commitSucceedView;

/** 预约课程数据  */
@property (nonatomic, strong) NSArray *courseArray;
/** 预约时间数据  */
@property (nonatomic, strong) NSArray *timeArray;

@property (nonatomic, strong) NSString *cid;
@property (nonatomic, strong) NSString *tid;

@end

@implementation YCCommitInterviewViewController
- (UIButton *)confirmCommitButton
{
    if (!_confirmCommitButton) {
        _confirmCommitButton = [[UIButton alloc] init];
        _confirmCommitButton.backgroundColor = [UIColor yc_colorWithUInt:0x5270a7];
        _confirmCommitButton.layer.cornerRadius = 35 / 2;
        _confirmCommitButton.layer.masksToBounds = YES;
        [_confirmCommitButton setTitle:@"确认提交" forState:UIControlStateNormal];
        _confirmCommitButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_confirmCommitButton addTarget:self action:@selector(didTouchUpInsideConfirmCommitButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmCommitButton;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self yc_addBackBarButtonItem];
    self.navigationItem.title = @"预约考试";
    [self makeView];
    [self loadData];
    
}

- (void)makeView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.alwaysBounceVertical = YES;
    // 添加单击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScrollView:)];
    [self.scrollView addGestureRecognizer:tap];
    self.scrollView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    [self.view addSubview:self.scrollView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    self.containerView = containerView;
    [self.scrollView addSubview:containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];

    // 考试科目
    UILabel *examCourseLabel = [[UILabel alloc] init];
    examCourseLabel.font = [UIFont systemFontOfSize:14];
    examCourseLabel.textColor = [UIColor yc_colorWithUInt:0x4d4d4d];
    examCourseLabel.text = @"考试科目";
    [containerView addSubview:examCourseLabel];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    UITextField *examCourseField = [[UITextField alloc] init];
    examCourseField.backgroundColor = [UIColor whiteColor];
    examCourseField.layer.borderWidth = 1;
    examCourseField.layer.borderColor = [UIColor yc_colorWithUInt:0xf0f0f0].CGColor;
    examCourseField.layer.cornerRadius = 5;
    examCourseField.layer.masksToBounds = YES;
    examCourseField.font = [UIFont systemFontOfSize:13];
    examCourseField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入考试科目" attributes:@{NSForegroundColorAttributeName:[UIColor yc_colorWithUInt:0xb3b3b3],NSFontAttributeName:[UIFont systemFontOfSize:13]}];
    examCourseField.leftView = leftView;
    examCourseField.leftViewMode = UITextFieldViewModeAlways;
    examCourseField.delegate = self;
    [containerView addSubview:examCourseField];
    self.examCourseField = examCourseField;
    
    // 考试时间
    UILabel *examTimeLabel = [[UILabel alloc] init];
    examTimeLabel.font = [UIFont systemFontOfSize:14];
    examTimeLabel.textColor = [UIColor yc_colorWithUInt:0x4d4d4d];
    examTimeLabel.text = @"考试时间";
    [containerView addSubview:examTimeLabel];
    
    YCExamTimeField *examTimeField= [[YCExamTimeField alloc] init];
    examTimeField.delegate = self;
    examTimeField.returnKeyType = UIReturnKeyDone;
    [containerView addSubview:examTimeField];
    self.examTimeField = examTimeField;
    
    
    // 确认提交
    [containerView addSubview:self.confirmCommitButton];
    
    [examCourseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(containerView).offset(heightFromIphone6Height(124));
        make.left.equalTo(containerView).offset(46);
    }];
    
    [examCourseField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(examCourseLabel.mas_bottom).offset(7);
        make.left.equalTo(examCourseLabel);
        make.right.equalTo(containerView).offset(-46);
        make.height.equalTo(@43);
    }];
    
    
    [examTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(examCourseField.mas_bottom).offset(21);
        make.left.equalTo(examCourseField);
    }];
    
    [examTimeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(examTimeLabel.mas_bottom).offset(7);
        make.left.right.height.equalTo(examCourseField);
    }];
    
    [self.confirmCommitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(examTimeField.mas_bottom).offset(heightFromIphone6Height(240));
        make.centerX.equalTo(self.containerView);
        make.left.equalTo(self.containerView).offset(50);
        make.right.equalTo(self.containerView).offset(-50);
        make.height.equalTo(@35);
    }];
    
    [containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.confirmCommitButton.mas_bottom).offset(heightFromIphone6Height(54));
    }];
    
    [self.scrollView layoutIfNeeded];
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.containerView.frame));

}

#pragma mark - 数据处理
- (void)loadData
{
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPIInterviewExam params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        NSArray *courseArray = response.data[@"course"];
        NSArray *timeArray = response.data[@"timeexam"];
        self.courseArray = [YCBookCourseModel yc_objectArrayWithKeyValuesArray:courseArray];
        self.timeArray = [YCBookTimeModel yc_objectArrayWithKeyValuesArray:timeArray];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
    }];
}


#pragma mark - 点击事件
- (void)didTouchUpInsideConfirmCommitButton:(UIButton *)button
{
    YCLogFunc
    if(self.cid == nil)
    {
        [YCAlertView showMsg:@"请选择考试科目"];
        return;
    }
    
    if(self.tid == nil)
    {
        [YCAlertView showMsg:@"请选择考试时间"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"提交中..."];
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             @"tid":self.tid,
                             @"cid":self.cid,
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPIInterviewExamCommit params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        YCLog(@"提交成功");
        [SVProgressHUD dismiss];
        [[[YCInterviewCommitSucceedView alloc] init] showSucceedMessge:@"" inView:self.view completeBlock:^{
            YCLog(@"回调了11111111");
            [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:NO];
        }];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"提交失败:%@",error.localizedDescription);
        [SVProgressHUD dismiss];
    }];
    
}

#pragma mark - UITextFieldDelegate
// 开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    YCLogFunc
    if (textField == self.examCourseField) {
        if (self.courseArray == nil) {
            [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请检查是否联网" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil] show];
            return;
        }
    } else {
        if (self.timeArray == nil) {
            [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请检查是否联网" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil] show];
            return;
        }
    }
    [textField resignFirstResponder];
    if (textField == self.examCourseField) {//课程
        NSMutableArray *courseArray = [NSMutableArray array];
        for (NSInteger i = self.courseArray.count - 1; i>=0; i--) {
            YCBookCourseModel *model = self.courseArray[i];
            [courseArray addObject:model.cname];
        }
        LXActionSheet *actionSheet = [[LXActionSheet alloc] initWithCancelButtonTitle:@"取消" otherButtonTitlesArray:courseArray];
        [actionSheet showInView:self.view mask:YES click:^(NSUInteger index, LXActionSheet *sheet) {
            YCBookCourseModel *model = self.courseArray[self.courseArray.count - index];
            self.cid = model.cid;
            self.examCourseField.text = model.cname;
        } cancel:nil];
    } else {//时间
        NSMutableArray *timeArray = [NSMutableArray array];
        for (NSInteger i = self.timeArray.count - 1; i>=0; i--) {
            YCBookTimeModel *model = self.timeArray[i];
            [timeArray addObject:model.ttime_str];
        }
        LXActionSheet *actionSheet = [[LXActionSheet alloc] initWithCancelButtonTitle:@"取消" otherButtonTitlesArray:timeArray];
        [actionSheet showInView:self.view mask:YES click:^(NSUInteger index, LXActionSheet *sheet) {
            YCBookTimeModel *model = self.timeArray[self.timeArray.count - index];
            self.tid = model.tid;
            self.examTimeField.text = model.ttime_str;
//            [self.confirmCommitButton sendActionsForControlEvents:UIControlEventTouchUpInside];
        } cancel:nil];
    }
    
}

// 键盘确认
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    YCLogFunc
    [self.view endEditing:YES];
    return YES;
}

#pragma mark - 手势
- (void)tapScrollView:(UITapGestureRecognizer *)tap
{
    YCLogFunc
    [self.view endEditing:YES];
}





@end
